package com.jiguang.shangcheng.servlet;

import com.jiguang.shangcheng.dao.AdminDao;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/admin/admin_deleteProduct")
public class adminDelectProduct extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");
        //获取参数
        String id = req.getParameter("id");
        String cp = req.getParameter("cp");
        String cz = req.getParameter("cz");

        if(Integer.parseInt(cz) == 1){
            //删除该用户
            boolean result = AdminDao.deleteProduct(Integer.parseInt(id));
            if(result){
                resp.sendRedirect("admin_product?cp="+cp);
            }else{
                PrintWriter out = resp.getWriter();
                out.println("<script>alert('删除失败');location.href='/admin/admin_product"+cp+"';</script>");
            }
        }else if(Integer.parseInt(cz) == 2){
            //下架
            boolean result = AdminDao.allowProduct(Integer.parseInt(id));
            if(result){
                resp.sendRedirect("admin_product?cp="+cp);
            }else{
                PrintWriter out = resp.getWriter();
                out.println("<script>alert('下架失败');location.href='/admin/admin_product"+cp+"';</script>");
            }
        }else {
            boolean result = AdminDao.downProduct(Integer.parseInt(id));
            if(result){
                resp.sendRedirect("admin_product?cp="+cp);
            }else{
                PrintWriter out = resp.getWriter();
                out.println("<script>alert('上架失败');location.href='/admin/admin_product"+cp+"';</script>");
            }
        }
    }
}
